package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goodsLimit.GoodsLimitModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_RECOVER_ENERGY, desc = "恢复体力")
public class DunHuangShikuRecoverEnergyCmd extends AbstractCommand {

    private int goodsId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuRecoverEnergyReqMsg req = DunHuangShiKuProto.DunHuangShiKuRecoverEnergyReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        goodsId = req.getGoodsId();
        ret = DunHuangShiKuMgr.canEnterActivitying(player,activityId);
        if(ret != 0){
            return;
        }
        if(req.getNum() <= 0 ){
            ret = GameErrorCode.E_BAG_USE_GOODS_COUNT_LESS_ONE;
            return;
        }
        Property property = null;
        if (DunHuangShiKuMgr.getShowLimitedUseStrengthItemIdList().contains(goodsId)) {
            // 如果是限制道具,先判断上限
            long goodsUseCount = player.getModule(GoodsLimitModule.class).getGoodsUseCount(goodsId, activityId);
            if (DunHuangShiKuMgr.getShowLimitedUseNum(goodsId) != -1 && goodsUseCount + req.getNum() > DunHuangShiKuMgr.getShowLimitedUseNum(goodsId)) {
                // 超过限制
                ret = GameErrorCode.E_BAG_USE_GOODS_LIMIT;
                return;
            }
            property = new Property(goodsId, BigInteger.valueOf(req.getNum()));
        } else if (goodsId == DunHuangShiKuMgr.DUNHUANGSHIKU_ENERGY_ITEM_ID) {
            property = new Property(goodsId, BigInteger.valueOf(req.getNum()));
        } else {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);
        if (DunHuangShiKuMgr.getShowLimitedUseStrengthItemIdList().contains(goodsId)) {
            // 发跨服之前记录一下使用的数量，如果失败在取消预扣除时再处理
            player.getModule(GoodsLimitModule.class).updateGoodsUseCount(goodsId, activityId, req.getNum());
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_DUN_HUANG_SHI_KU_RECOVER_ENERGY, req.toBuilder(), req.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_RECOVER_ENERGY, builder);
        }
    }
}
